package com.lin.fragments;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lin.activity.MyCollectionActivity;
import com.lin.activity.MyDynamicActivity;
import com.lin.activity.QRCodeCreateActivity;
import com.lin.activity.SettingActivity;
import com.lin.utils.Parameters;
import com.lin.utils.Session;
import com.lin.utils.Utils;
import com.ly.quickdev.library.activity.DevBaseActivity;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.PinterestCircleBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.yunfeng.supercall.R;
import com.yunfeng.supercall.activity.BaseActivity;
import com.yunfeng.supercall.activity.PersonalDetailActivity;
import com.yunfeng.supercall.bean.Constants;
import com.yunfeng.supercall.bean.UserBean;

public class MineFragment extends Fragment implements OnClickListener, Constants {
	View mView;
	private LinearLayout mSettingLinearLayout;
	private LinearLayout mMyHomeLinearLayout;
	private LinearLayout mMyCardLinearLayout;
	private LinearLayout mMyCollectionLinearLayout;
	private LinearLayout mLinearLayoutInviteFriends;
	private ImageView mImageViewAvatar;
	DisplayImageOptions options;
	DisplayImageOptions options1;
	DisplayImageOptions options2;
	DisplayImageOptions options3;
	DisplayImageOptions options4;

	private TextView mTextViewUserName;
	private TextView mTextViewPhoneNumber;
	private FrameLayout mFrameLayoutHeader;

	private ImageView mImageView1;
	private ImageView mImageView2;
	private ImageView mImageView3;
	private ImageView mImageView4;
	private ImageView mImageView5;

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub

		Log.i("TAG", "onCreateView");

		if (mView == null) {
			mView = inflater.inflate(R.layout.fragment_mine, null);
		}
		// 缓存的rootView需要判断是否已经被加过parent，如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
		ViewGroup parent = (ViewGroup) mView.getParent();
		if (parent != null) {
			parent.removeView(mView);
		}

		mFrameLayoutHeader = (FrameLayout) mView.findViewById(R.id.framelayout);

		Session mSession = Session.getSession();
		mSession.init(getActivity(), true);

		UserBean user = mSession.getmUserBean();

		options = new DisplayImageOptions.Builder()
				.displayer(
						new PinterestCircleBitmapDisplayer(getResources()
								.getDimensionPixelSize(R.dimen.corners_avatar)))
				.showImageOnFail(R.drawable.bg_share_header)
				.showImageOnLoading(R.drawable.bg_share_header)
				.cacheInMemory(true).cacheOnDisk(true).considerExifParams(true)
				.bitmapConfig(Bitmap.Config.RGB_565).build();

		options1 = new DisplayImageOptions.Builder()
				.displayer(
						new RoundedBitmapDisplayer(getResources()
								.getDimensionPixelSize(R.dimen.corners_avatar)))
				.cacheInMemory(true).cacheOnDisk(true).considerExifParams(true)
				.bitmapConfig(Bitmap.Config.RGB_565).build();

		options2 = new DisplayImageOptions.Builder()
				.displayer(
						new RoundedBitmapDisplayer(getResources()
								.getDimensionPixelSize(R.dimen.corners_avatar)))
				.cacheInMemory(true).cacheOnDisk(true).considerExifParams(true)
				.bitmapConfig(Bitmap.Config.RGB_565).build();

		options3 = new DisplayImageOptions.Builder()
				.displayer(
						new RoundedBitmapDisplayer(getResources()
								.getDimensionPixelSize(R.dimen.corners_avatar)))
				.cacheInMemory(true).cacheOnDisk(true).considerExifParams(true)
				.bitmapConfig(Bitmap.Config.RGB_565).build();

		options4 = new DisplayImageOptions.Builder()
				.displayer(
						new RoundedBitmapDisplayer(getResources()
								.getDimensionPixelSize(R.dimen.corners_avatar)))
				.cacheInMemory(true).cacheOnDisk(true).considerExifParams(true)
				.bitmapConfig(Bitmap.Config.RGB_565).build();
		mImageView1 = (ImageView) mView.findViewById(R.id.imageview1);
		mImageView2 = (ImageView) mView.findViewById(R.id.imageview2);
		mImageView3 = (ImageView) mView.findViewById(R.id.imageview3);
		mImageView4 = (ImageView) mView.findViewById(R.id.imageview4);
		mImageView5 = (ImageView) mView.findViewById(R.id.imageview5);

		mSettingLinearLayout = (LinearLayout) mView.findViewById(R.id.setting);
		mMyHomeLinearLayout = (LinearLayout) mView.findViewById(R.id.my_home);
		mMyCardLinearLayout = (LinearLayout) mView.findViewById(R.id.my_card);
		mMyCollectionLinearLayout = (LinearLayout) mView.findViewById(R.id.my_collection);
		mLinearLayoutInviteFriends = (LinearLayout) mView
				.findViewById(R.id.invite_friend);

		mImageViewAvatar = (ImageView) mView.findViewById(R.id.avatar);
		mTextViewUserName = (TextView) mView.findViewById(R.id.user_name);
		mTextViewPhoneNumber = (TextView) mView.findViewById(R.id.num);
		try {

			mTextViewUserName.setText(user.getNick_name());
		} catch (NullPointerException e) {
			getActivity().finish();
		}
		if (user == null || mTextViewPhoneNumber == null) {
			getActivity().finish();
		}
		try {
			mTextViewPhoneNumber.setText(user.getSuperNumber() + "");
		} catch (NullPointerException e) {
			getActivity().finish();
		}
		try {
			if (user == null) {
				getActivity().finish();
			}
			ImageLoader.getInstance().displayImage(
					Parameters.SERVER_PIC_URL + user.getPhoto(),
					mImageViewAvatar, options);
		} catch (Exception e) {

		}

		mFrameLayoutHeader.setOnClickListener(this);
		mSettingLinearLayout.setOnClickListener(this);
		mMyHomeLinearLayout.setOnClickListener(this);
		mMyCardLinearLayout.setOnClickListener(this);
		mMyCollectionLinearLayout.setOnClickListener(this);
		mLinearLayoutInviteFriends.setOnClickListener(this);
		ImageLoader.getInstance().displayImage(
				"drawable://" + R.drawable.ic_my_homepage, mImageView1,
				options1);
		ImageLoader.getInstance().displayImage(
				"drawable://" + R.drawable.ic_my_card, mImageView2, options2);
		ImageLoader.getInstance().displayImage(
				"drawable://" + R.drawable.ic_share_invite, mImageView4,
				options3);
		ImageLoader.getInstance().displayImage(
				"drawable://" + R.drawable.ic_mine_setting, mImageView5,
				options4);
		return mView;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (mSettingLinearLayout == v) {
			startActivity(new Intent(getActivity(), SettingActivity.class));
		} else if (mMyHomeLinearLayout == v) {
			Intent intent = new Intent(getActivity(), MyDynamicActivity.class);
			int uid = BaseActivity.getUser(
					getActivity().getApplicationContext()).getUserid();
			intent.putExtra(KEY_DATA, uid);
			startActivity(intent);
		} else if (mMyCardLinearLayout == v) {
			Intent intent = new Intent(getActivity(),
					QRCodeCreateActivity.class);
			mImageViewAvatar.setDrawingCacheEnabled(true);
			intent.putExtra("avatar", mImageViewAvatar.getDrawingCache());
			startActivity(intent);
		} else if (mMyCollectionLinearLayout == v) {
			startActivity(new Intent(getActivity(), MyCollectionActivity.class));
		} else if (mLinearLayoutInviteFriends == v) {
			View content = getActivity().getLayoutInflater().inflate(
					R.layout.dialog_share, null);
			Dialog mDialog = DevBaseActivity.getDialog(getActivity(), content,
					null);
			Utils.setUpImage(getActivity(), content);
		} else if (mFrameLayoutHeader == v) {
			startActivity(new Intent(getActivity(),
					PersonalDetailActivity.class));
		}
	}

	/*private void setUpImage(View v) {
		if (v instanceof ImageView) {
			v.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					
					 * int img = v.getId(); String platform = null; switch
					 * (v.getId()) { case R.id.weibo: platform =
					 * TencentWeibo.NAME; break; case R.id.wechat_circle:
					 * platform = WechatMoments.NAME; break; case
					 * R.id.wechat_friends: platform = Wechat.NAME; break; case
					 * R.id.qq: platform = QQ.NAME; break; case R.id.sms_share:
					 * break; } Utils.share(getActivity(),
					 * (PlatformActionListener) MineFragment.this, platform);
					 

					int img = v.getId();
					try {
						if (img == R.id.weibo) {
							ShareManager.shareToWeiBo(getActivity());
						} else if (img == R.id.wechat_circle) {
							int userid = BaseActivity.getUser(getActivity())
									.getUserid();
							Utils.shareWechatCircle(getActivity(),
									MineFragment.this, userid);
						} else if (img == R.id.wechat_friends) {
							int userid = BaseActivity.getUser(getActivity())
									.getUserid();
							Utils.shareWechatToFriends(getActivity(),
									MineFragment.this, userid);
						} else if (img == R.id.qq) {
							ShareManager.shareToQQ(getActivity());
						} else if (img == R.id.sms_share) {
							int userid = BaseActivity.getUser(getActivity())
									.getUserid();
							Utils.sendSMS(getActivity(), getResources()
									.getString(R.string.sms_share, userid));
						}
					} catch (NullPointerException e) {

					}
				}
			});
		} else if (v instanceof ViewGroup) {
			for (int i = 0; i < ((ViewGroup) v).getChildCount(); i++) {
				setUpImage(((ViewGroup) v).getChildAt(i));
			}
		}
	}*/

	/*@Override
	public void onCancel(Platform arg0, int arg1) {
		Message msg = new Message();
		msg.what = 0;
		UIHandler.sendMessage(msg, this);
	}

	@Override
	public void onComplete(Platform plat, int action,
			HashMap<String, Object> res) {
		Message msg = new Message();
		msg.arg1 = 1;
		msg.arg2 = action;
		msg.obj = plat;
		UIHandler.sendMessage(msg, this);
	}

	@Override
	public void onError(Platform arg0, int arg1, Throwable arg2) {
		Message msg = new Message();
		msg.what = 1;
		UIHandler.sendMessage(msg, this);
	}

	@Override
	public boolean handleMessage(Message msg) {
		// TODO Auto-generated method stub
		int what = msg.what;
		if (what == 1) {
			Toast.makeText(getActivity(), "分享失败", Toast.LENGTH_SHORT).show();
		}
		return false;
	}*/

}